package org.brisling.medical.service;

import java.util.Date;
import java.util.List;

import org.brisling.authorize.shiro.ShiroSecurityService;
import org.brisling.base.domain.ListDomain;
import org.brisling.base.domain.RetDomain;
import org.brisling.common.ConstSysParam;
import org.brisling.medical.domain.MedicalRecipel;
import org.brisling.medical.queryService.QueryRecipelService;
import org.brisling.medical.repository.RecipelRepo;
import org.brisling.medical.service.base.BaseMedicalFlowService;
import org.brisling.workflow.domain.VOHistoric;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


@RestController
@RequestMapping("/medicalRecipel")
/**
 * 医疗服务处方类质询服務service
 * @Author : jackson wang
 * @Date   : 2016年1月25日 下午4:18:01
 * @version: 1.0
 */
public class RecipelService extends BaseMedicalFlowService<MedicalRecipel>{
	
			
	@SuppressWarnings("unused")
	@Autowired
	private RecipelRepo baseFlowRepo;	
	
	
	@Autowired
	private ShiroSecurityService shiroSecurityService;	
	
	
	public RecipelService(){
		this.clazz = MedicalRecipel.class;
		this.moduleName = ConstSysParam.MODULE_RECIPEL;		
	}
	
		
	/**
	 * 获取业务主页面 
	 * @return
	 */
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return getBizMainPage();
	}
	
	/**
	 * 获取业务编辑页面
	 * @return
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return getBizEditPage();
	}
	
	/**
	 * 返回缺陷记录流程实例id的执行记录
	 * @param procInstId 流程实例id
	 * @return List<HistoricTaskInstance> 流程实例执行记录
	 */
	@RequestMapping("/flowHistorics.action")
	public List<VOHistoric> getHistoricByProcInstId(@RequestParam(value="flowInstId",required=false) String procInstId){
		return super.getHistoricByProcInstId(procInstId);
	}
	
	/**
	 * 返回缺陷记录流程实例id的执行记录
	 * @param procInstId 流程实例id
	 * @return List<HistoricTaskInstance> 流程实例执行记录
	 */
	@RequestMapping("/flowHistoricsListDomain.action")
	public ListDomain<VOHistoric> getBizHistoricListDomainByProcInstId(@RequestParam(value="flowInstId",required=false) String procInstId){
		return super.getHistoricListDomainByProcInstId(procInstId);
	}
	/**
	 * 分页查询用户列表页面
	 * @param deptid
	 * @param page
	 * @param rows
	 * @param sort
	 * @return
	 */
	@RequestMapping("/flowListDomain.action")
	public ListDomain<MedicalRecipel> getListDomain(
			@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="rows",required=false) Integer rows,
			@RequestParam(value="sort",required=false) String sort,
			@RequestParam(value="flowtype",required=false) String flowtype,
			@RequestParam(value="fullBizcode",required=false) String fullBizcode){
		
			if(flowtype==null)
				return super.getListDomain(page, rows, sort, fullBizcode);
			else{
				switch(flowtype){
				
				case "0":
					return super.getPagedFinishedFlowRows(page, rows, sort, fullBizcode);
					
				default:
					return null;
				}
			}
			
			
	}
	
	
	/**
	 * 分页查询用户列表页面
	 * @param deptid
	 * @param page
	 * @param rows
	 * @param sort
	 * @return
	 */
	@RequestMapping("/flowList.action")
	public List<MedicalRecipel> getPagedFlowRows(
			@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="rows",required=false) Integer rows,
			@RequestParam(value="sort",required=false) String sort,			
			@RequestParam(value="fullBizcode",required=false) String fullBizcode
			){		
		
		return super.getPagedFlowRows(page, rows, sort, fullBizcode);			
	}
	
	/**
	 * 删除人员信息
	 * @param id
	 */
	@RequestMapping("/flowDel.action")
	public RetDomain delRow(@RequestParam(value="id",required=false) Long id,@RequestParam(value="ids",required=false) String ids){
		RetDomain domain = new RetDomain();
		if(id!=null){
			if(super.deleteTask(id, null)){
				domain.setSuccessMsg("200");
				return domain;
			}else{
				domain.setErrorMsg("500");
				return domain;
			}
		}else{
			if(ids!=null){
				Integer count =super.deleteTaskList(ids, null);
				domain.setMsg("删除 " + count + " 条记录成功!");
			}
		}
		
		return domain;
		
	}

	
	/**
	 * 保存缺陷记录
	 * @param id
	 */
	@RequestMapping("/flowSave.action")
	public MedicalRecipel save(@RequestBody String body){
		MedicalRecipel obj = bodyToObj(body, MedicalRecipel.class);	
		if(obj!=null){
			if(obj.getBizno()==null || obj.getBizno().trim().length()<=0){
				obj.setBizno(qryService.generateBizno());
			}
		}
		MedicalRecipel recipel= this.saveFlowEntity(shiroSecurityService.getCurrentUserid().toString(),obj);
		if((recipel!=null) && (recipel.getId()!=null)){
			recipel = qryService.getOne(recipel.getId());
		}
		return recipel;
	}
	
	/**
	 * 自动保存医疗服务质询记录
	 * @param userid
	 * @param obj
	 * @param inquiryDate
	 * @return
	 */

	public MedicalRecipel autoSave(String userid,MedicalRecipel obj){
		Date _date = obj.getCreateTime() ==null?obj.getOuthospitalat():obj.getCreateTime();
		obj.setBizno(qryService.generateBizno(_date));
		MedicalRecipel recipel= this.saveFlowEntity(userid,obj);
		if((recipel!=null) && (recipel.getId()!=null)){
			recipel = qryService.getOne(recipel.getId());
		}
		return recipel;
	}
	
	public MedicalRecipel autoSaveNew(String userid,MedicalRecipel obj){
		
		Date _date = obj.getCreateTime() ==null?obj.getOuthospitalat():obj.getCreateTime();
		obj.setBizno(qryService.generateBizno(_date));
		MedicalRecipel recipel= this.saveFlowEntity(userid,obj);
		if((recipel!=null) && (recipel.getId()!=null)){
			recipel = qryService.getOne(recipel.getId());
		}
		return recipel;
	}
	
	/**
	 * 签收记录
	 * @param flowInstId	流程实例id
	 * @param flowTaskId	流程节点id
	 * @param signInfo		签收信息
	 * @param body
	 * @return
	 */
	@RequestMapping("/flowSign.action")
	public ListDomain<Object> saveSingInfo(@RequestBody String body){
		
		
		return this.saveSignInfo(shiroSecurityService.getCurrentUserid().toString(), body);
	}
	
	
	@Autowired
	private QueryRecipelService qryService;
	
	
	@RequestMapping("/flowTaskComplete.action")
	public String completeTask(@RequestBody String body){
		MedicalRecipel recipel =this.save(body);
		
		if(recipel!=null){
			
			return super.completeTaskAndCandidateByEntity(
					shiroSecurityService.getCurrentUserid().toString(),
					recipel, 
					"签收");
		}
		
		return "500";
	}
	
	
	/**
	 * 自动发送任务方法
	 * @param userId	任务发送人员id
	 * @param domain	发送的业务单据实体
	 * @return
	 */
	public String completeTask(String userId,MedicalRecipel domain){
		domain =this.autoSave(userId,domain);
		
		if(domain!=null && userId!=null){			
			return super.completeTaskAndCandidateByEntity(
					userId,domain,"签收");
		}		
		return "500";		
	}
	
	

	
	@RequestMapping("/flowSendback.action")
	public RetDomain saveSendbackTask(@RequestBody String body){
		MedicalRecipel recipel =this.save(body);
		if(recipel!=null){
			return super.saveSendBack(
					shiroSecurityService.getCurrentUserid().toString(), 
					recipel, null, null);
			
		}
		RetDomain domain = new RetDomain();
		domain.setErrorMsg("业务保存失败");
		return domain;
	}
	
	public void updateData(){
		
	}
	
	
}
